#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_5_clicked()
{
    if(ui->lineEdit->text() != "" && ui->lineEdit_2->text() != "" ){
        bool ok;
        char devip[64] ={};
        memcpy(devip,ui->lineEdit->text().toLatin1().data(),ui->lineEdit->text().toLatin1().length());
        int port = ui->lineEdit_2->text().toInt(&ok,10);

        mClient.abort();
        mClient.connectToHost(devip, port);
        if (!mClient.waitForConnected(5000))
        {
            qDebug() << "mClient.waitForConnected err!";
            return;
        }
    }
    return;
}


void MainWindow::on_pushButton_clicked()
{
    std::string cmd = cmd_moving_str("1");
    mClient.write(cmd.c_str(), cmd.length());
}


void MainWindow::on_pushButton_2_clicked()
{
    std::string cmd = cmd_moving_str("2");
    mClient.write(cmd.c_str(), cmd.length());
}

void MainWindow::on_pushButton_3_clicked()
{
    std::string cmd = cmd_moving_str("3");
    mClient.write(cmd.c_str(), cmd.length());
}

void MainWindow::on_pushButton_4_clicked()
{
    std::string cmd = cmd_moving_str("4");
    mClient.write(cmd.c_str(), cmd.length());
}

std::string MainWindow::cmd_moving_str(std::string num)
{
    rapidjson::StringBuffer strBuf;
    rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
    writer.StartObject();

    writer.Key("Action");
    writer.String("Moving");
    writer.Key("PointNum");
    writer.String(num.c_str());
    writer.EndObject();

    std::string str = strBuf.GetString();
    str += "$&";
    return str;
}

